<!DOCTYPE HTML>
<html lang="en" >
    <!-- Start book Python爬虫课程讲义 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Requests模块 | Python爬虫课程讲义</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="BigCat">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-tbfed-pagefooter/footer.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../file/part02/2.html" />
    
    
    <link rel="prev" href="../../file/part01/1.7.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="1.8"
        data-chapter-title="Requests模块"
        data-filepath="file/part01/1.8.md"
        data-basepath="../.."
        data-revision="Thu Feb 09 2017 09:48:59 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        传智播客Python学院爬虫课程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="file/part01/1.html">
            
                
                    <a href="../../file/part01/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        爬虫原理与数据抓取
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="file/part01/1.1.html">
            
                
                    <a href="../../file/part01/1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        (了解)通用爬虫和聚焦爬虫
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="file/part01/1.2.html">
            
                
                    <a href="../../file/part01/1.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        (复习)HTTP/HTTPS的请求与响应
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="file/part01/1.3.html">
            
                
                    <a href="../../file/part01/1.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        HTTP/HTTPS抓包工具-Fiddler
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="file/part01/1.4.html">
            
                
                    <a href="../../file/part01/1.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        urllib2模块的基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="file/part01/1.5.html">
            
                
                    <a href="../../file/part01/1.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        urllib2：GET请求和POST请求
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="file/part01/1.6.html">
            
                
                    <a href="../../file/part01/1.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        urllib2：Handler处理器和自定义Opener
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="file/part01/1.7.html">
            
                
                    <a href="../../file/part01/1.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        urllib2：URLError与HTTPError
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1.8" data-path="file/part01/1.8.html">
            
                
                    <a href="../../file/part01/1.8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                        Requests模块
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="file/part02/2.html">
            
                
                    <a href="../../file/part02/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        非结构化数据与结构化数据提取
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="file/part02/2.1.html">
            
                
                    <a href="../../file/part02/2.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        正则表达式re模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="file/part02/2.2.html">
            
                
                    <a href="../../file/part02/2.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        案例：使用正则表达式的爬虫
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="file/part02/2.3.html">
            
                
                    <a href="../../file/part02/2.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        XPath与lxml类库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="file/part02/2.4.html">
            
                
                    <a href="../../file/part02/2.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        案例：使用XPath的爬虫
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="file/part02/2.5.html">
            
                
                    <a href="../../file/part02/2.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        BeautifulSoup4 解析器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="file/part02/2.6.html">
            
                
                    <a href="../../file/part02/2.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        案例：使用bs4的爬虫
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="file/part02/2.7.html">
            
                
                    <a href="../../file/part02/2.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        JSON模块与JsonPath
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="file/part02/2.8.html">
            
                
                    <a href="../../file/part02/2.8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        糗事百科案例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="file/part02/2.9.html">
            
                
                    <a href="../../file/part02/2.9.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.9.</b>
                        
                        多线程爬虫案例
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="file/part03/3.html">
            
                
                    <a href="../../file/part03/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        动态HTML处理和机器图像识别
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="file/part03/3.1.html">
            
                
                    <a href="../../file/part03/3.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        动态HTML介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="file/part03/3.2.html">
            
                
                    <a href="../../file/part03/3.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        Selenium与PhantomJS
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="file/part03/3.3.html">
            
                
                    <a href="../../file/part03/3.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        案例一：网站模拟登录
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="file/part03/3.4.html">
            
                
                    <a href="../../file/part03/3.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        案例二：动态页面模拟点击
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="file/part03/3.5.html">
            
                
                    <a href="../../file/part03/3.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        案例三：执行JavaScript语句
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="file/part03/3.6.html">
            
                
                    <a href="../../file/part03/3.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        机器视觉与Tesseract介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="file/part03/3.7.html">
            
                
                    <a href="../../file/part03/3.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        处理一些格式规范的文字
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="file/part03/3.8.html">
            
                
                    <a href="../../file/part03/3.8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        案例：尝试对验证码进行机器识别处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="file/part03/3.9.html">
            
                
                    <a href="../../file/part03/3.9.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        机器学习：训练Tesseract
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="file/part04/4.html">
            
                
                    <a href="../../file/part04/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        Scrapy框架
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="file/part04/4.1.html">
            
                
                    <a href="../../file/part04/4.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        配置安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="file/part04/4.2.html">
            
                
                    <a href="../../file/part04/4.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        入门案例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="file/part04/4.3.html">
            
                
                    <a href="../../file/part04/4.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Scrapy Shell
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="file/part04/4.4.html">
            
                
                    <a href="../../file/part04/4.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        Item Pipeline
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="file/part04/4.5.html">
            
                
                    <a href="../../file/part04/4.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        Spiders
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="file/part04/4.6.html">
            
                
                    <a href="../../file/part04/4.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        CrawlSpiders
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="file/part04/4.7.html">
            
                
                    <a href="../../file/part04/4.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        Request/Response
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="file/part04/4.8.html">
            
                
                    <a href="../../file/part04/4.8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.8.</b>
                        
                        Downloader Middlewares
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="file/part04/4.9.html">
            
                
                    <a href="../../file/part04/4.9.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.</b>
                        
                        Settings
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="file/part05/5.html">
            
                
                    <a href="../../file/part05/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        Scrapy实战项目
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="file/part05/5.1.html">
            
                
                    <a href="../../file/part05/5.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        (案例一)手机App抓包爬虫
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="file/part05/5.2.html">
            
                
                    <a href="../../file/part05/5.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        (案例二)阳光热线问政平台爬虫
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="file/part05/5.3.html">
            
                
                    <a href="../../file/part05/5.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        (案例三)新浪网分类资讯爬虫
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="file/part05/5.4.html">
            
                
                    <a href="../../file/part05/5.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        (案例四)图片下载器爬虫
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="file/part05/5.5.html">
            
                
                    <a href="../../file/part05/5.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        (案例五)将数据保存在MongoDB中
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="file/part05/5.6.html">
            
                
                    <a href="../../file/part05/5.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        (案例六)三种scrapy模拟登陆策略
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="file/part05/5.7.html">
            
                
                    <a href="../../file/part05/5.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        附：通过Fiddler进行手机抓包方法
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="file/part06/6.html">
            
                
                    <a href="../../file/part06/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        scrapy-redis分布式组件
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="file/part06/6.1.html">
            
                
                    <a href="../../file/part06/6.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        源码分析参考：Connection
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="file/part06/6.2.html">
            
                
                    <a href="../../file/part06/6.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        源码分析参考：Dupefilter
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="file/part06/6.3.html">
            
                
                    <a href="../../file/part06/6.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        源码分析参考：Picklecompat
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="file/part06/6.4.html">
            
                
                    <a href="../../file/part06/6.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        源码分析参考：Pipelines
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="file/part06/6.5.html">
            
                
                    <a href="../../file/part06/6.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        源码分析参考：Queue
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="file/part06/6.6.html">
            
                
                    <a href="../../file/part06/6.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        源码分析参考：Scheduler
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="file/part06/6.7.html">
            
                
                    <a href="../../file/part06/6.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                        源码分析参考：Spider
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="file/part07/7.html">
            
                
                    <a href="../../file/part07/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        scrapy-redis实战
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="file/part07/7.1.html">
            
                
                    <a href="../../file/part07/7.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        源码自带项目说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="file/part07/7.2.html">
            
                
                    <a href="../../file/part07/7.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        有缘网分布式爬虫项目1
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="file/part07/7.3.html">
            
                
                    <a href="../../file/part07/7.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        有缘网分布式爬虫项目2
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="file/part07/7.4.html">
            
                
                    <a href="../../file/part07/7.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        处理Redis里的数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="file/part07/7.5.html">
            
                
                    <a href="../../file/part07/7.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        尝试改写新浪网分类资讯爬虫1
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.6" data-path="file/part07/7.6.html">
            
                
                    <a href="../../file/part07/7.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.6.</b>
                        
                        尝试改写新浪网分类资讯爬虫2
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.7" data-path="file/part07/7.7.html">
            
                
                    <a href="../../file/part07/7.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.7.</b>
                        
                        IT桔子分布式项目1
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.8" data-path="file/part07/7.8.html">
            
                
                    <a href="../../file/part07/7.8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.8.</b>
                        
                        IT桔子分布式项目2
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="file/duanzi/duanzi.html">
            
                
                    <a href="../../file/duanzi/duanzi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        课余段子
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Python爬虫课程讲义</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="requests-&#x8BA9;-http-&#x670D;&#x52A1;&#x4EBA;&#x7C7B;">Requests: &#x8BA9; HTTP &#x670D;&#x52A1;&#x4EBA;&#x7C7B;</h1>
<p>&#x867D;&#x7136;Python&#x7684;&#x6807;&#x51C6;&#x5E93;&#x4E2D; urllib2 &#x6A21;&#x5757;&#x5DF2;&#x7ECF;&#x5305;&#x542B;&#x4E86;&#x5E73;&#x5E38;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x7684;&#x5927;&#x591A;&#x6570;&#x529F;&#x80FD;&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x7684; API &#x4F7F;&#x7528;&#x8D77;&#x6765;&#x8BA9;&#x4EBA;&#x611F;&#x89C9;&#x4E0D;&#x592A;&#x597D;&#xFF0C;&#x800C; Requests &#x81EA;&#x79F0; &#x201C;HTTP for Humans&#x201D;&#xFF0C;&#x8BF4;&#x660E;&#x4F7F;&#x7528;&#x66F4;&#x7B80;&#x6D01;&#x65B9;&#x4FBF;&#x3002;</p>
<blockquote>
<p>Requests &#x552F;&#x4E00;&#x7684;&#x4E00;&#x4E2A;&#x975E;&#x8F6C;&#x57FA;&#x56E0;&#x7684; Python HTTP &#x5E93;&#xFF0C;&#x4EBA;&#x7C7B;&#x53EF;&#x4EE5;&#x5B89;&#x5168;&#x4EAB;&#x7528;&#xFF1A;&#xFF09;</p>
</blockquote>
<p>Requests &#x7EE7;&#x627F;&#x4E86;urllib2&#x7684;&#x6240;&#x6709;&#x7279;&#x6027;&#x3002;Requests&#x652F;&#x6301;HTTP&#x8FDE;&#x63A5;&#x4FDD;&#x6301;&#x548C;&#x8FDE;&#x63A5;&#x6C60;&#xFF0C;&#x652F;&#x6301;&#x4F7F;&#x7528;cookie&#x4FDD;&#x6301;&#x4F1A;&#x8BDD;&#xFF0C;&#x652F;&#x6301;&#x6587;&#x4EF6;&#x4E0A;&#x4F20;&#xFF0C;&#x652F;&#x6301;&#x81EA;&#x52A8;&#x786E;&#x5B9A;&#x54CD;&#x5E94;&#x5185;&#x5BB9;&#x7684;&#x7F16;&#x7801;&#xFF0C;&#x652F;&#x6301;&#x56FD;&#x9645;&#x5316;&#x7684; URL &#x548C; POST &#x6570;&#x636E;&#x81EA;&#x52A8;&#x7F16;&#x7801;&#x3002;</p>
<h4 id="requests-&#x7684;&#x5E95;&#x5C42;&#x5B9E;&#x73B0;&#x5176;&#x5B9E;&#x5C31;&#x662F;-urllib3">requests &#x7684;&#x5E95;&#x5C42;&#x5B9E;&#x73B0;&#x5176;&#x5B9E;&#x5C31;&#x662F; urllib3</h4>
<p>Requests&#x7684;&#x6587;&#x6863;&#x975E;&#x5E38;&#x5B8C;&#x5907;&#xFF0C;&#x4E2D;&#x6587;&#x6587;&#x6863;&#x4E5F;&#x76F8;&#x5F53;&#x4E0D;&#x9519;&#x3002;Requests&#x80FD;&#x5B8C;&#x5168;&#x6EE1;&#x8DB3;&#x5F53;&#x524D;&#x7F51;&#x7EDC;&#x7684;&#x9700;&#x6C42;&#xFF0C;&#x652F;&#x6301;Python 2.6&#x2014;3.5&#xFF0C;&#x800C;&#x4E14;&#x80FD;&#x5728;PyPy&#x4E0B;&#x5B8C;&#x7F8E;&#x8FD0;&#x884C;&#x3002;</p>
<p>&#x5F00;&#x6E90;&#x5730;&#x5740;&#xFF1A;<a href="https://github.com/kennethreitz/requests" target="_blank">https://github.com/kennethreitz/requests</a></p>
<p>&#x4E2D;&#x6587;&#x6587;&#x6863; API&#xFF1A;
<a href="http://docs.python-requests.org/zh_CN/latest/index.html" target="_blank">http://docs.python-requests.org/zh_CN/latest/index.html</a></p>
<h2 id="&#x5B89;&#x88C5;&#x65B9;&#x5F0F;">&#x5B89;&#x88C5;&#x65B9;&#x5F0F;</h2>
<p>&#x5229;&#x7528; pip &#x5B89;&#x88C5; &#x6216;&#x8005;&#x5229;&#x7528; easy_install &#x90FD;&#x53EF;&#x4EE5;&#x5B8C;&#x6210;&#x5B89;&#x88C5;&#xFF1A;</p>
<pre><code class="lang-j">$ pip install requests

$ easy_install requests
</code></pre>
<h2 id="&#x57FA;&#x672C;get&#x8BF7;&#x6C42;&#xFF08;headers&#x53C2;&#x6570;-&#x548C;-parmas&#x53C2;&#x6570;&#xFF09;">&#x57FA;&#x672C;GET&#x8BF7;&#x6C42;&#xFF08;headers&#x53C2;&#x6570; &#x548C; parmas&#x53C2;&#x6570;&#xFF09;</h2>
<h5 id="1-&#x6700;&#x57FA;&#x672C;&#x7684;get&#x8BF7;&#x6C42;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x7528;get&#x65B9;&#x6CD5;">1. &#x6700;&#x57FA;&#x672C;&#x7684;GET&#x8BF7;&#x6C42;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x7528;get&#x65B9;&#x6CD5;</h5>
<pre><code class="lang-python">response = requests.get(<span class="hljs-string">&quot;http://www.baidu.com/&quot;</span>)

<span class="hljs-comment"># &#x4E5F;&#x53EF;&#x4EE5;&#x8FD9;&#x4E48;&#x5199;</span>
<span class="hljs-comment"># response = requests.request(&quot;get&quot;, &quot;http://www.baidu.com/&quot;)</span>
</code></pre>
<h5 id="2-&#x6DFB;&#x52A0;-headers-&#x548C;-&#x67E5;&#x8BE2;&#x53C2;&#x6570;">2. &#x6DFB;&#x52A0; headers &#x548C; &#x67E5;&#x8BE2;&#x53C2;&#x6570;</h5>
<p>&#x5982;&#x679C;&#x60F3;&#x6DFB;&#x52A0; headers&#xFF0C;&#x53EF;&#x4EE5;&#x4F20;&#x5165;<code>headers</code>&#x53C2;&#x6570;&#x6765;&#x589E;&#x52A0;&#x8BF7;&#x6C42;&#x5934;&#x4E2D;&#x7684;headers&#x4FE1;&#x606F;&#x3002;&#x5982;&#x679C;&#x8981;&#x5C06;&#x53C2;&#x6570;&#x653E;&#x5728;url&#x4E2D;&#x4F20;&#x9012;&#xFF0C;&#x53EF;&#x4EE5;&#x5229;&#x7528; <code>params</code> &#x53C2;&#x6570;&#x3002;</p>
<pre><code class="lang-python">
<span class="hljs-keyword">import</span> requests

kw = {<span class="hljs-string">&apos;wd&apos;</span>:<span class="hljs-string">&apos;&#x957F;&#x57CE;&apos;</span>}

headers = {<span class="hljs-string">&quot;User-Agent&quot;</span>: <span class="hljs-string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36&quot;</span>}

<span class="hljs-comment"># params &#x63A5;&#x6536;&#x4E00;&#x4E2A;&#x5B57;&#x5178;&#x6216;&#x8005;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x67E5;&#x8BE2;&#x53C2;&#x6570;&#xFF0C;&#x5B57;&#x5178;&#x7C7B;&#x578B;&#x81EA;&#x52A8;&#x8F6C;&#x6362;&#x4E3A;url&#x7F16;&#x7801;&#xFF0C;&#x4E0D;&#x9700;&#x8981;urlencode()</span>
response = requests.get(<span class="hljs-string">&quot;http://www.baidu.com/s?&quot;</span>, params = kw, headers = headers)

<span class="hljs-comment"># &#x67E5;&#x770B;&#x54CD;&#x5E94;&#x5185;&#x5BB9;&#xFF0C;response.text &#x8FD4;&#x56DE;&#x7684;&#x662F;Unicode&#x683C;&#x5F0F;&#x7684;&#x6570;&#x636E;</span>
<span class="hljs-keyword">print</span> response.text

<span class="hljs-comment"># &#x67E5;&#x770B;&#x54CD;&#x5E94;&#x5185;&#x5BB9;&#xFF0C;response.content&#x8FD4;&#x56DE;&#x7684;&#x5B57;&#x8282;&#x6D41;&#x6570;&#x636E;</span>
<span class="hljs-keyword">print</span> respones.content

<span class="hljs-comment"># &#x67E5;&#x770B;&#x5B8C;&#x6574;url&#x5730;&#x5740;</span>
<span class="hljs-keyword">print</span> response.url

<span class="hljs-comment"># &#x67E5;&#x770B;&#x54CD;&#x5E94;&#x5934;&#x90E8;&#x5B57;&#x7B26;&#x7F16;&#x7801;</span>
<span class="hljs-keyword">print</span> response.encoding

<span class="hljs-comment"># &#x67E5;&#x770B;&#x54CD;&#x5E94;&#x7801;</span>
<span class="hljs-keyword">print</span> response.status_code
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;</p>
<pre><code class="lang-j"><!DOCTYPE html><!--STATUS OK--><html>...<title>&#x957F;&#x57CE;_&#x767E;&#x5EA6;&#x641C;&#x7D22;</title>...

<!DOCTYPE html><!--STATUS OK--><html>...<title>&#x957F;&#x57CE;_&#x767E;&#x5EA6;&#x641C;&#x7D22;</title>...

&apos;http://www.baidu.com/s?wd=%E9%95%BF%E5%9F%8E&apos;

&apos;utf-8&apos;

200
</html></html></code></pre>
<blockquote>
<ul>
<li><p>&#x4F7F;&#x7528;response.text &#x65F6;&#xFF0C;Requests &#x4F1A;&#x57FA;&#x4E8E; HTTP &#x54CD;&#x5E94;&#x7684;&#x6587;&#x672C;&#x7F16;&#x7801;&#x81EA;&#x52A8;&#x89E3;&#x7801;&#x54CD;&#x5E94;&#x5185;&#x5BB9;&#xFF0C;&#x5927;&#x591A;&#x6570; Unicode &#x5B57;&#x7B26;&#x96C6;&#x90FD;&#x80FD;&#x88AB;&#x65E0;&#x7F1D;&#x5730;&#x89E3;&#x7801;&#x3002;</p>
</li>
<li><p>&#x4F7F;&#x7528;response.content &#x65F6;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x670D;&#x52A1;&#x5668;&#x54CD;&#x5E94;&#x6570;&#x636E;&#x7684;&#x539F;&#x59CB;&#x4E8C;&#x8FDB;&#x5236;&#x5B57;&#x8282;&#x6D41;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x4FDD;&#x5B58;&#x56FE;&#x7247;&#x7B49;&#x4E8C;&#x8FDB;&#x5236;&#x6587;&#x4EF6;&#x3002;</p>
</li>
</ul>
</blockquote>
<h2 id="&#x57FA;&#x672C;post&#x8BF7;&#x6C42;&#xFF08;data&#x53C2;&#x6570;&#xFF09;">&#x57FA;&#x672C;POST&#x8BF7;&#x6C42;&#xFF08;data&#x53C2;&#x6570;&#xFF09;</h2>
<h5 id="1-&#x6700;&#x57FA;&#x672C;&#x7684;get&#x8BF7;&#x6C42;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x7528;post&#x65B9;&#x6CD5;">1. &#x6700;&#x57FA;&#x672C;&#x7684;GET&#x8BF7;&#x6C42;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x7528;post&#x65B9;&#x6CD5;</h5>
<pre><code class="lang-python">response = requests.post(<span class="hljs-string">&quot;http://www.baidu.com/&quot;</span>, data = data)
</code></pre>
<h5 id="2-&#x4F20;&#x5165;data&#x6570;&#x636E;">2. &#x4F20;&#x5165;data&#x6570;&#x636E;</h5>
<p>&#x5BF9;&#x4E8E; POST &#x8BF7;&#x6C42;&#x6765;&#x8BF4;&#xFF0C;&#x6211;&#x4EEC;&#x4E00;&#x822C;&#x9700;&#x8981;&#x4E3A;&#x5B83;&#x589E;&#x52A0;&#x4E00;&#x4E9B;&#x53C2;&#x6570;&#x3002;&#x90A3;&#x4E48;&#x6700;&#x57FA;&#x672C;&#x7684;&#x4F20;&#x53C2;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x5229;&#x7528; <code>data</code> &#x8FD9;&#x4E2A;&#x53C2;&#x6570;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests

formdata = {
    <span class="hljs-string">&quot;type&quot;</span>:<span class="hljs-string">&quot;AUTO&quot;</span>,
    <span class="hljs-string">&quot;i&quot;</span>:<span class="hljs-string">&quot;i love python&quot;</span>,
    <span class="hljs-string">&quot;doctype&quot;</span>:<span class="hljs-string">&quot;json&quot;</span>,
    <span class="hljs-string">&quot;xmlVersion&quot;</span>:<span class="hljs-string">&quot;1.8&quot;</span>,
    <span class="hljs-string">&quot;keyfrom&quot;</span>:<span class="hljs-string">&quot;fanyi.web&quot;</span>,
    <span class="hljs-string">&quot;ue&quot;</span>:<span class="hljs-string">&quot;UTF-8&quot;</span>,
    <span class="hljs-string">&quot;action&quot;</span>:<span class="hljs-string">&quot;FY_BY_ENTER&quot;</span>,
    <span class="hljs-string">&quot;typoResult&quot;</span>:<span class="hljs-string">&quot;true&quot;</span>
}

url = <span class="hljs-string">&quot;http://fanyi.youdao.com/translate?smartresult=dict&amp;smartresult=rule&amp;smartresult=ugc&amp;sessionFrom=null&quot;</span>

headers={ <span class="hljs-string">&quot;User-Agent&quot;</span>: <span class="hljs-string">&quot;Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36&quot;</span>}

response = requests.post(url, data = formdata, headers = headers)

<span class="hljs-keyword">print</span> response.text

<span class="hljs-comment"># &#x5982;&#x679C;&#x662F;json&#x6587;&#x4EF6;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x663E;&#x793A;</span>
<span class="hljs-keyword">print</span> response.json()
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;</p>
<pre><code class="lang-j">{&quot;type&quot;:&quot;EN2ZH_CN&quot;,&quot;errorCode&quot;:0,&quot;elapsedTime&quot;:2,&quot;translateResult&quot;:[[{&quot;src&quot;:&quot;i love python&quot;,&quot;tgt&quot;:&quot;&#x6211;&#x559C;&#x6B22;python&quot;}]],&quot;smartResult&quot;:{&quot;type&quot;:1,&quot;entries&quot;:[&quot;&quot;,&quot;&#x8086;&#x6587;&quot;,&quot;&#x9AD8;&#x5FB7;&#x7EB3;&quot;]}}

{u&apos;errorCode&apos;: 0, u&apos;elapsedTime&apos;: 0, u&apos;translateResult&apos;: [[{u&apos;src&apos;: u&apos;i love python&apos;, u&apos;tgt&apos;: u&apos;\u6211\u559c\u6b22python&apos;}]], u&apos;smartResult&apos;: {u&apos;type&apos;: 1, u&apos;entries&apos;: [u&apos;&apos;, u&apos;\u8086\u6587&apos;, u&apos;\u9ad8\u5fb7\u7eb3&apos;]}, u&apos;type&apos;: u&apos;EN2ZH_CN&apos;}
</code></pre>
<h2 id="&#x4EE3;&#x7406;&#xFF08;proxies&#x53C2;&#x6570;&#xFF09;">&#x4EE3;&#x7406;&#xFF08;proxies&#x53C2;&#x6570;&#xFF09;</h2>
<p>&#x5982;&#x679C;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x4EE3;&#x7406;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4E3A;&#x4EFB;&#x610F;&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;&#x63D0;&#x4F9B; <code>proxies</code> &#x53C2;&#x6570;&#x6765;&#x914D;&#x7F6E;&#x5355;&#x4E2A;&#x8BF7;&#x6C42;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests

<span class="hljs-comment"># &#x6839;&#x636E;&#x534F;&#x8BAE;&#x7C7B;&#x578B;&#xFF0C;&#x9009;&#x62E9;&#x4E0D;&#x540C;&#x7684;&#x4EE3;&#x7406;</span>
proxies = {
  <span class="hljs-string">&quot;http&quot;</span>: <span class="hljs-string">&quot;http://12.34.56.79:9527&quot;</span>,
  <span class="hljs-string">&quot;https&quot;</span>: <span class="hljs-string">&quot;http://12.34.56.79:9527&quot;</span>,
}

response = requests.get(<span class="hljs-string">&quot;http://www.baidu.com&quot;</span>, proxies = proxies)
<span class="hljs-keyword">print</span> response.text
</code></pre>
<p>&#x4E5F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x672C;&#x5730;&#x73AF;&#x5883;&#x53D8;&#x91CF; <code>HTTP_PROXY</code> &#x548C; <code>HTTPS_PROXY</code> &#x6765;&#x914D;&#x7F6E;&#x4EE3;&#x7406;&#xFF1A;</p>
<pre><code class="lang-bash"><span class="hljs-built_in">export</span> HTTP_PROXY=<span class="hljs-string">&quot;http://12.34.56.79:9527&quot;</span>
<span class="hljs-built_in">export</span> HTTPS_PROXY=<span class="hljs-string">&quot;https://12.34.56.79:9527&quot;</span>
</code></pre>
<h2 id="&#x79C1;&#x5BC6;&#x4EE3;&#x7406;&#x9A8C;&#x8BC1;&#xFF08;&#x7279;&#x5B9A;&#x683C;&#x5F0F;&#xFF09;-&#x548C;-web&#x5BA2;&#x6237;&#x7AEF;&#x9A8C;&#x8BC1;&#xFF08;auth-&#x53C2;&#x6570;&#xFF09;">&#x79C1;&#x5BC6;&#x4EE3;&#x7406;&#x9A8C;&#x8BC1;&#xFF08;&#x7279;&#x5B9A;&#x683C;&#x5F0F;&#xFF09; &#x548C; Web&#x5BA2;&#x6237;&#x7AEF;&#x9A8C;&#x8BC1;&#xFF08;auth &#x53C2;&#x6570;&#xFF09;</h2>
<p>urllib2 &#x8FD9;&#x91CC;&#x7684;&#x505A;&#x6CD5;&#x6BD4;&#x8F83;&#x590D;&#x6742;&#xFF0C;requests&#x53EA;&#x9700;&#x8981;&#x4E00;&#x6B65;&#xFF1A;</p>
<h4 id="&#x79C1;&#x5BC6;&#x4EE3;&#x7406;">&#x79C1;&#x5BC6;&#x4EE3;&#x7406;</h4>
<pre><code class="lang-python">
<span class="hljs-keyword">import</span> requests

<span class="hljs-comment"># &#x5982;&#x679C;&#x4EE3;&#x7406;&#x9700;&#x8981;&#x4F7F;&#x7528;HTTP Basic Auth&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E0B;&#x9762;&#x8FD9;&#x79CD;&#x683C;&#x5F0F;&#xFF1A;</span>
proxy = { <span class="hljs-string">&quot;http&quot;</span>: <span class="hljs-string">&quot;mr_mao_hacker:sffqry9r@61.158.163.130:16816&quot;</span> }

response = requests.get(<span class="hljs-string">&quot;http://www.baidu.com&quot;</span>, proxies = proxy)

<span class="hljs-keyword">print</span> response.text
</code></pre>
<h4 id="web&#x5BA2;&#x6237;&#x7AEF;&#x9A8C;&#x8BC1;">web&#x5BA2;&#x6237;&#x7AEF;&#x9A8C;&#x8BC1;</h4>
<p>&#x5982;&#x679C;&#x662F;Web&#x5BA2;&#x6237;&#x7AEF;&#x9A8C;&#x8BC1;&#xFF0C;&#x9700;&#x8981;&#x6DFB;&#x52A0; auth = (&#x8D26;&#x6237;&#x540D;, &#x5BC6;&#x7801;)</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests

auth=(<span class="hljs-string">&apos;test&apos;</span>, <span class="hljs-string">&apos;123456&apos;</span>)

response = requests.get(<span class="hljs-string">&apos;http://192.168.199.107&apos;</span>, auth = auth)

<span class="hljs-keyword">print</span> response.text
</code></pre>
<blockquote>
<p>urllib2 &#x6CEA;&#x5954;...</p>
</blockquote>
<h2 id="cookies-&#x548C;-sission">Cookies &#x548C; Sission</h2>
<h3 id="cookies">Cookies</h3>
<p>&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x54CD;&#x5E94;&#x4E2D;&#x5305;&#x542B;&#x4E86;cookie&#xFF0C;&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5229;&#x7528; cookies&#x53C2;&#x6570;&#x62FF;&#x5230;&#xFF1A;</p>
<pre><code class="lang-python">
<span class="hljs-keyword">import</span> requests

response = requests.get(<span class="hljs-string">&quot;http://www.baidu.com/&quot;</span>)

<span class="hljs-comment"># 7. &#x8FD4;&#x56DE;CookieJar&#x5BF9;&#x8C61;:</span>
cookiejar = response.cookies

<span class="hljs-comment"># 8. &#x5C06;CookieJar&#x8F6C;&#x4E3A;&#x5B57;&#x5178;&#xFF1A;</span>
cookiedict = requests.utils.dict_from_cookiejar(cookiejar)

<span class="hljs-keyword">print</span> cookiejar

<span class="hljs-keyword">print</span> cookiedict
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">&lt;RequestsCookieJar[&lt;Cookie BDORZ=<span class="hljs-number">27315</span> <span class="hljs-keyword">for</span> .baidu.com/&gt;]&gt;

{<span class="hljs-string">&apos;BDORZ&apos;</span>: <span class="hljs-string">&apos;27315&apos;</span>}
</code></pre>
<h3 id="sission">Sission</h3>
<p>&#x5728; requests &#x91CC;&#xFF0C;session&#x5BF9;&#x8C61;&#x662F;&#x4E00;&#x4E2A;&#x975E;&#x5E38;&#x5E38;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x4EE3;&#x8868;&#x4E00;&#x6B21;&#x7528;&#x6237;&#x4F1A;&#x8BDD;&#xFF1A;&#x4ECE;&#x5BA2;&#x6237;&#x7AEF;&#x6D4F;&#x89C8;&#x5668;&#x8FDE;&#x63A5;&#x670D;&#x52A1;&#x5668;&#x5F00;&#x59CB;&#xFF0C;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#x6D4F;&#x89C8;&#x5668;&#x4E0E;&#x670D;&#x52A1;&#x5668;&#x65AD;&#x5F00;&#x3002;</p>
<p>&#x4F1A;&#x8BDD;&#x80FD;&#x8BA9;&#x6211;&#x4EEC;&#x5728;&#x8DE8;&#x8BF7;&#x6C42;&#x65F6;&#x5019;&#x4FDD;&#x6301;&#x67D0;&#x4E9B;&#x53C2;&#x6570;&#xFF0C;&#x6BD4;&#x5982;&#x5728;&#x540C;&#x4E00;&#x4E2A; Session &#x5B9E;&#x4F8B;&#x53D1;&#x51FA;&#x7684;&#x6240;&#x6709;&#x8BF7;&#x6C42;&#x4E4B;&#x95F4;&#x4FDD;&#x6301; cookie &#x3002;</p>
<h4 id="&#x5B9E;&#x73B0;&#x4EBA;&#x4EBA;&#x7F51;&#x767B;&#x5F55;">&#x5B9E;&#x73B0;&#x4EBA;&#x4EBA;&#x7F51;&#x767B;&#x5F55;</h4>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests

<span class="hljs-comment"># 1. &#x521B;&#x5EFA;session&#x5BF9;&#x8C61;&#xFF0C;&#x53EF;&#x4EE5;&#x4FDD;&#x5B58;Cookie&#x503C;</span>
ssion = requests.session()

<span class="hljs-comment"># 2. &#x5904;&#x7406; headers</span>
headers = {<span class="hljs-string">&quot;User-Agent&quot;</span>: <span class="hljs-string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36&quot;</span>}

<span class="hljs-comment"># 3. &#x9700;&#x8981;&#x767B;&#x5F55;&#x7684;&#x7528;&#x6237;&#x540D;&#x548C;&#x5BC6;&#x7801;</span>
data = {<span class="hljs-string">&quot;email&quot;</span>:<span class="hljs-string">&quot;mr_mao_hacker@163.com&quot;</span>, <span class="hljs-string">&quot;password&quot;</span>:<span class="hljs-string">&quot;alarmchime&quot;</span>}  

<span class="hljs-comment"># 4. &#x53D1;&#x9001;&#x9644;&#x5E26;&#x7528;&#x6237;&#x540D;&#x548C;&#x5BC6;&#x7801;&#x7684;&#x8BF7;&#x6C42;&#xFF0C;&#x5E76;&#x83B7;&#x53D6;&#x767B;&#x5F55;&#x540E;&#x7684;Cookie&#x503C;&#xFF0C;&#x4FDD;&#x5B58;&#x5728;ssion&#x91CC;</span>
ssion.post(<span class="hljs-string">&quot;http://www.renren.com/PLogin.do&quot;</span>, data = data)

<span class="hljs-comment"># 5. ssion&#x5305;&#x542B;&#x7528;&#x6237;&#x767B;&#x5F55;&#x540E;&#x7684;Cookie&#x503C;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8BBF;&#x95EE;&#x90A3;&#x4E9B;&#x767B;&#x5F55;&#x540E;&#x624D;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x7684;&#x9875;&#x9762;</span>
response = ssion.get(<span class="hljs-string">&quot;http://www.renren.com/410043129/profile&quot;</span>)

<span class="hljs-comment"># 6. &#x6253;&#x5370;&#x54CD;&#x5E94;&#x5185;&#x5BB9;</span>
<span class="hljs-keyword">print</span> response.text
</code></pre>
<p><img src="../images/requestrenren.png" alt=""></p>
<h2 id="&#x5904;&#x7406;https&#x8BF7;&#x6C42;-ssl&#x8BC1;&#x4E66;&#x9A8C;&#x8BC1;">&#x5904;&#x7406;HTTPS&#x8BF7;&#x6C42; SSL&#x8BC1;&#x4E66;&#x9A8C;&#x8BC1;</h2>
<p>Requests&#x4E5F;&#x53EF;&#x4EE5;&#x4E3A;HTTPS&#x8BF7;&#x6C42;&#x9A8C;&#x8BC1;SSL&#x8BC1;&#x4E66;&#xFF1A;</p>
<ul>
<li>&#x8981;&#x60F3;&#x68C0;&#x67E5;&#x67D0;&#x4E2A;&#x4E3B;&#x673A;&#x7684;SSL&#x8BC1;&#x4E66;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; verify &#x53C2;&#x6570;&#xFF08;&#x4E5F;&#x53EF;&#x4EE5;&#x4E0D;&#x5199;&#xFF09;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests
response = requests.get(<span class="hljs-string">&quot;https://www.baidu.com/&quot;</span>, verify=<span class="hljs-keyword">True</span>)

<span class="hljs-comment"># &#x4E5F;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x4E0D;&#x5199;</span>
<span class="hljs-comment"># response = requests.get(&quot;https://www.baidu.com/&quot;)</span>
<span class="hljs-keyword">print</span> r.text
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code>&lt;!DOCTYPE html&gt;
&lt;!--STATUS OK--&gt;&lt;html&gt; &lt;head&gt;&lt;meta http-equiv=content-type content=text/html;charset=utf-8&gt;&lt;meta http-equiv=X-UA-Compatible content=IE=Edge&gt;&#x767E;&#x5EA6;&#x4E00;&#x4E0B;&#xFF0C;&#x4F60;&#x5C31;&#x77E5;&#x9053; ....
</code></pre><ul>
<li>&#x5982;&#x679C;SSL&#x8BC1;&#x4E66;&#x9A8C;&#x8BC1;&#x4E0D;&#x901A;&#x8FC7;&#xFF0C;&#x6216;&#x8005;&#x4E0D;&#x4FE1;&#x4EFB;&#x670D;&#x52A1;&#x5668;&#x7684;&#x5B89;&#x5168;&#x8BC1;&#x4E66;&#xFF0C;&#x5219;&#x4F1A;&#x62A5;&#x51FA;SSLError&#xFF0C;&#x636E;&#x8BF4; 12306 &#x8BC1;&#x4E66;&#x662F;&#x81EA;&#x5DF1;&#x505A;&#x7684;&#xFF1A;</li>
</ul>
<p><img src="../images/12306zhengshu.png" alt=""></p>
<p>&#x6765;&#x6D4B;&#x8BD5;&#x4E00;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests
response = requests.get(<span class="hljs-string">&quot;https://www.12306.cn/mormhweb/&quot;</span>)
<span class="hljs-keyword">print</span> response.text
</code></pre>
<p>&#x679C;&#x7136;&#xFF1A;</p>
<p><code>SSLError: (&quot;bad handshake: Error([(&apos;SSL routines&apos;, &apos;ssl3_get_server_certificate&apos;, &apos;certificate verify failed&apos;)],)&quot;,)</code></p>
<p><strong>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x60F3;&#x8DF3;&#x8FC7; 12306 &#x7684;&#x8BC1;&#x4E66;&#x9A8C;&#x8BC1;&#xFF0C;&#x628A; verify &#x8BBE;&#x7F6E;&#x4E3A; False &#x5C31;&#x53EF;&#x4EE5;&#x6B63;&#x5E38;&#x8BF7;&#x6C42;&#x4E86;&#x3002;</strong></p>
<pre><code class="lang-python">r = requests.get(<span class="hljs-string">&quot;https://www.12306.cn/mormhweb/&quot;</span>, verify = <span class="hljs-keyword">False</span>)
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; BigCat all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x300C;Revision Time:
2017-01-14 21:11:29&#x300D;
</span></footer>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../file/part01/1.7.html" class="navigation navigation-prev " aria-label="Previous page: urllib2：URLError与HTTPError"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../file/part02/2.html" class="navigation navigation-next " aria-label="Next page: 非结构化数据与结构化数据提取"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"disqus":{"shortName":"gitbookuse"},"github":{"url":"https://github.com/dododream"},"search-pro":{"cutWordLib":"nodejieba","defineWord":["gitbook-use"]},"sharing":{"weibo":true,"facebook":true,"twitter":true,"google":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"tbfed-pagefooter":{"copyright":"Copyright © BigCat","modify_label":"「Revision Time:","modify_format":"YYYY-MM-DD HH:mm:ss」"},"baidu":{"token":"ff100361cdce95dd4c8fb96b4009f7bc"},"sitemap":{"hostname":"http://www.treenewbee.top"},"donate":{"wechat":"http://weixin.png","alipay":"http://alipay.png","title":"","button":"赏","alipayText":"支付宝打赏","wechatText":"微信打赏"},"edit-link":{"base":"https://github.com/dododream/edit","label":"Edit This Page"},"splitter":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book Python爬虫课程讲义 -->
</html>
